package com.ksd.modules.advert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.core.exception.KsdBusinessException;
import com.ksd.common.core.response.ResponseEnum;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.advert.entity.AdminAdvert;
import com.ksd.modules.advert.entity.AdminAdvertment;
import com.ksd.modules.advert.mapper.AdminAdvertementMapper;
import com.ksd.modules.advert.po.AdminAdvertMentSavePO;
import com.ksd.modules.advert.po.AdminAdvertMentSearchPO;
import com.ksd.modules.advert.service.IAdminAdvertService;
import com.ksd.modules.advert.service.IAdminAdvertisementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class AdminAdvertisementService extends BaseServiceImpl<AdminAdvertementMapper, AdminAdvertment> implements IAdminAdvertisementService {
    @Resource
    private IAdminAdvertService adminAdvertService;
    @Override
    public Page<AdminAdvertment> findAdminAdvertment(AdminAdvertMentSearchPO adminAdvertMentSearchPO) {
        LambdaQueryWrapper<AdminAdvertment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminAdvertment::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminAdvertMentSearchPO.getDeleted()), AdminAdvertment::getIsDeleted,adminAdvertMentSearchPO.getDeleted());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(adminAdvertMentSearchPO.getKeyword()), AdminAdvertment::getNickname, adminAdvertMentSearchPO.getKeyword());
        lambdaQueryWrapper.eq(Objects.nonNull(adminAdvertMentSearchPO.getStatus()), AdminAdvertment::getStatus, adminAdvertMentSearchPO.getStatus());

        lambdaQueryWrapper.orderByDesc(AdminAdvertment::getCreateTime);
        // 查询返回
        Page<AdminAdvertment> page = new Page<>(adminAdvertMentSearchPO.getPageNo(), adminAdvertMentSearchPO.getPageSize());
        return this.page(page, lambdaQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateAdminAdvertment(AdminAdvertMentSavePO adminAdvertMentSavePO) {
        AdminAdvertment adminAdvertment = CopyConvertUtils.copyProperties(adminAdvertMentSavePO, AdminAdvertment.class);
        adminAdvertment.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminAdvertment.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return this.saveOrUpdate(adminAdvertment);
    }

    @Override
    public boolean deleteAdminAdvertmentById(Long id) {
        LambdaQueryWrapper<AdminAdvert> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminAdvert::getAdvertiserId,id);
        lambdaQueryWrapper.eq(AdminAdvert::getIsDeleted,DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminAdvert::getStatus,StatusEnums.STATUS.getCode());
        long count = adminAdvertService.count(lambdaQueryWrapper);
        if(count>0){
            throw new KsdBusinessException(ResponseEnum.ADVERTISEMENT_ERROR);
        }
        AdminAdvertment adminAdvertment=new AdminAdvertment();
        adminAdvertment.setIsDeleted(DeletedEnums.DELETED.getCode());
        adminAdvertment.setStatus(StatusEnums.UN_STATUS.getCode());
        adminAdvertment.setId(id);
        return this.updateById(adminAdvertment);
    }

    @Override
    public List<AdminAdvertment> getList() {
        LambdaQueryWrapper<AdminAdvertment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminAdvertment::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminAdvertment::getIsDeleted,DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminAdvertment::getStatus,StatusEnums.STATUS.getCode());
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public AdminAdvertment getAdminAdvertmentById(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean updateStatus(Long id, String column, Integer value) {
        if("status".equals(column)){
            UpdateWrapper<AdminAdvert> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("advertiser_id",id);
            updateWrapper.set("status",StatusEnums.UN_STATUS.getCode());
            adminAdvertService.update(updateWrapper);
        }
        return super.updateStatus(id, column, value);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminAdvertment data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setNickname(data.getNickname()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
